package com.ccf.business.database.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ccf.business.database.model.Datasource;
import com.ccf.business.database.model.TableCache;
import com.ccf.business.database.service.IDatasourceService;
import com.ccf.business.database.service.ITableCacheService;
import com.service.database.config.DataBase;
import com.service.database.config.DatabaseConfig;
import com.service.database.model.DataModel;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 * 远程服务提供者
 * @author xianggj
 * @Date 2022/2/21 18:24
 **/
@RestController
@RequestMapping("/remote")
@RequiredArgsConstructor
public class RemoteController {

    private final ITableCacheService tableCacheService;

    private final IDatasourceService datasourceService;

    /**
     * 查询数据
     *
     * @param sid
     * @return
     */
    @GetMapping(value = "/getTableData")
    public DataModel getTableData(@RequestParam(value = "sid", required = true) String sid,
                                  @RequestParam(value = "tableName", required = true) String tableName) {
        try {
            DataModel detail = datasourceService.getTableData(sid, tableName, null, null);
            return detail;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存缓存表
     *
     * @param sid
     * @return
     */
    @PostMapping(value = "/saveTableCache")
    public void saveTableCache(@RequestParam(value = "sid", required = true) String sid,
                               @RequestParam(value = "tableName", required = true) String tableName,
                               @RequestParam(value = "remark", required = false) String remark) {
        TableCache tableCache = new TableCache();
        tableCache.setSourceId(sid);
        tableCache.setTableName(tableName);
        tableCache.setRemark("决策树预测创表");
        tableCacheService.save(tableCache);
    }

    /**
     * 获取数据库配置
     *
     * @param sid
     * @return
     */
    @GetMapping(value = "/getConfig")
    public DatabaseConfig getConfig(@RequestParam(value = "sid", required = true) String sid,
                                    @RequestParam(value = "tableName", required = true) String tableName) {

        Datasource datasource = datasourceService.getById(sid);
        String driver = datasource.getDriver();
        String url = datasource.getUrl();
        String userName = datasource.getUserName();
        String pwd = datasource.getPwd();
        String schema = datasource.getSchemaName() == null ? "public" : datasource.getSchemaName();
        String dbName = datasource.getDatabaseName();
        //构造配置
        DatabaseConfig.DatabaseConfigBuilder builder = DatabaseConfig
                .DatabaseConfigBuilder
                .aDatabaseConfig()
                .withType(DataBase.getDataBaseByDriver(driver))
                .withUrl(url)
                .withSchema(schema)
                .withUserName(userName)
                .withDbName(dbName)
                .withUserPwd(pwd)
                .withDriver(driver);

        //设置表信息
        if(StrUtil.isNotEmpty(tableName)){
            builder.withTableName(tableName);
        }
        //重新生成URL
        return  builder.build();
    }

    /**
     * 获取数据库配置
     *
     * @param sid
     * @return
     */
    @GetMapping(value = "/getTableDesc")
    public List<Map<String, Object>> getTableDesc(@RequestParam(value = "sid", required = true) String sid,
                                                  @RequestParam(value = "tableName", required = true) String tableName) {

        try {
            List<Map<String, Object>> tableDesc = datasourceService.getTableDesc(sid, tableName);
            if (CollUtil.isEmpty(tableDesc)){
                return new ArrayList<>();
            }
            return tableDesc;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    /**
     * 查询不可操作的表
     *
     * @param sid
     * @return
     */
    @GetMapping(value = "/listTablesExcludeSelfBuilt")
    public List<String> listTablesExcludeSelfBuilt(@RequestParam(value = "sid", required = true) String sid) {

        try {
            List<String> tables = datasourceService.listTablesExcludeSelfBuilt(sid);
            if (CollUtil.isEmpty(tables)){
                return new ArrayList<>();
            }
            return tables;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }



}
